package com.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import com.bean.District;
import com.bean.House;
import com.bean.HouseType;
import com.bean.Street;
import com.bean.Type;
import com.bean.User;
import com.biz.DistrictBiz;
import com.biz.HouseBiz;
import com.biz.HouseTypeBiz;
import com.biz.StreetBiz;
import com.biz.TypeBiz;
import com.biz.UserBiz;
import com.biz.impl.DistrictBizImpl;
import com.biz.impl.HouseBizImpl;
import com.biz.impl.HouseTypeBizImpl;
import com.biz.impl.StreetBizImpl;
import com.biz.impl.TypeBizImpl;
import com.biz.impl.UserBizImpl;
import com.dao.DistrictDao;
import com.dao.HouseTypeDao;
import com.dao.TypeDao;
import com.dao.impl.DistrictDaoImpl;
import com.dao.impl.HouseTypeDaoImpl;
import com.dao.impl.TypeDaoImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class PublishAction extends ActionSupport implements ModelDriven<House> {
	private House house;
	private int houseTypeId;
	private int typeId;
	private int streetId;
	private int districtId;
	private List<HouseType> houseTypes;
	private List<District> districts;
	private List<Street> streets;
	private List<Type> types;
	private String result;

	@Override
	public String execute() {
		TypeBiz typeBiz = new TypeBizImpl();
		StreetBiz streetBiz = new StreetBizImpl();
		UserBiz userBiz = new UserBizImpl();
		HouseBiz houseBiz = new HouseBizImpl();
		HouseTypeBiz houseTypeBiz = new HouseTypeBizImpl();
		Map<String, Object> sessionMap = ActionContext.getContext().getSession();
		
		Type type = typeBiz.get(typeId);
		Street street = streetBiz.get(streetId);
		//User tempUser = (User) ActionContext.getContext().getSession().get("user");
		//User user = userBiz.get(tempUser.getId());
		User user = userBiz.get(2);
		HouseType houseType = houseTypeBiz.get(houseTypeId);
		String photoName = (String) sessionMap.get("photoName");
		
		house.setType(type);
		house.setStreet(street);
		house.setUser(user);
		house.setHouseType(houseType);
		house.setPubdate(new Date());
		house.setPhotoName(photoName != null ? photoName : "");
		houseBiz.add(house);
		return SUCCESS;
	}
	
	public String toPublish() {
		HouseTypeBiz houseTypeBiz = new HouseTypeBizImpl();
		houseTypes = houseTypeBiz.getAll();
		DistrictBiz districtBiz = new DistrictBizImpl();
		districts = districtBiz.getAll();
		TypeDao typeDao = new TypeDaoImpl();
		types = typeDao.getAll();
		return SUCCESS;
	}
	
	public String getStreet() throws JSONException {
		StreetBiz streetBiz = new StreetBizImpl();
		streets = streetBiz.getStreets(districtId);
		result = JSONUtil.serialize(streets);
		return SUCCESS;
	}
	
	public House getHouse() {
		return house;
	}

	public void setHouse(House house) {
		this.house = house;
	}

	public int getHouseTypeId() {
		return houseTypeId;
	}

	public void setHouseTypeId(int houseTypeId) {
		this.houseTypeId = houseTypeId;
	}

	public int getTypeId() {
		return typeId;
	}

	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}

	public int getStreetId() {
		return streetId;
	}

	public void setStreetId(int streetId) {
		this.streetId = streetId;
	}

	public int getDistrictId() {
		return districtId;
	}

	public void setDistrictId(int districtId) {
		this.districtId = districtId;
	}

	public List<HouseType> getHouseTypes() {
		return houseTypes;
	}

	public void setHouseTypes(List<HouseType> houseTypes) {
		this.houseTypes = houseTypes;
	}

	public List<District> getDistricts() {
		return districts;
	}

	public void setDistricts(List<District> districts) {
		this.districts = districts;
	}

	public List<Street> getStreets() {
		return streets;
	}

	public void setStreets(List<Street> streets) {
		this.streets = streets;
	}

	public List<Type> getTypes() {
		return types;
	}

	public void setTypes(List<Type> types) {
		this.types = types;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@Override
	public House getModel() {
		house = new House();
		return house;
	}
}
